/**
 * Created by huangxiaolu on 2014/4/8.
 */
var restify = require('restify');
var cluster = require('cluster');
var redis =require('redis');
//var redisClient = redis.createClient();
//var dao = require('./dao');

function respond(req,res,next){
//    console.log(JSON.stringify(req.headers));
    res.send('hola '+req.params.name);
    console.log('restify responding ');
}
function sleep(milliseconds) {
    var start = new Date().getTime();
    for (var i = 0; i < 1e7; i++) {
        if ((new Date().getTime() - start) > milliseconds){
            break;
        }
    }
}
function postRespond(req, res, next)
{
    sleep(100);
    res.setHeader('SOA20-Response-Status','Success');
    //res.contenttype='application/json;charset=utf-8';
    res.send({"filters":[{"type":1,"values":[{"id":"","name":"不限","val":"0"},{"id":"1","name":"1日","val":"2"},{"id":"2","name":"2日","val":"2"},{"id":"3","name":"3日","val":"1"},{"id":"4","name":"4日","val":"1"},{"id":"5","name":"5日","val":"3"}]},{"type":2,"values":[{"id":"","name":"不限","val":"0"},{"id":"33","name":"贵阳","val":"6"},{"id":"518","name":"安顺","val":"4"},{"id":"10195","name":"黄果树","val":"3"},{"id":"1770","name":"赤水","val":"2"},{"id":"491","name":"凯里","val":"2"},{"id":"204","name":"遵义","val":"2"},{"id":"10111","name":"西江苗寨","val":"2"},{"id":"521","name":"毕节","val":"1"}]},{"type":3,"values":[{"id":"","name":"不限","val":"0"}]}],"flag":0,"products":[{"currency":"RMB","dDateRmk":"每周三","prefFlag":"代理","flag":0,"imgUrls":"http://pkgpic.ctrip.com/images3/1/33/132x88/33_15_132x88.jpg","price":1399,"id":1710176,"name":"贵州黄果树+西江苗寨5日团队游(3钻)·金海雪山赏花 当地游","prmt":{"amt":0,"currency":"RMB","list":[],"type":0},"subtitle":"国家AAAAA级景区、亚洲第一大瀑布--黄果树！","prmtDiscount":0,"prmtRmk":""},{"currency":"RMB","dDateRmk":"天天出发","prefFlag":"代理","flag":2,"imgUrls":"http://pkgpic.ctrip.com/images3/1/518/132x88/518_01_132x88.jpg","price":398,"id":1730435,"name":"贵阳黄果树大瀑布、天星桥、陡坡塘纯玩一日游","prmt":{"amt":0,"currency":"RMB","list":[],"type":0},"subtitle":"为贵州旅游代言，全程绝无自费项目，包含景区环保车，真正一票到底！","prmtDiscount":0,"prmtRmk":""},{"currency":"RMB","dDateRmk":"04/03、04/04...","prefFlag":"代理","flag":0,"imgUrls":"http://pkgpic.ctrip.com/images3/1/521/132x88/521_21_s22587_132x88.JPG","price":1559,"id":1749228,"name":"贵州黄果树+毕节+安顺5日当地参团(3钻)·百里杜鹃 赏花探洞乐趣多","prmt":{"amt":0,"currency":"RMB","list":[],"type":0},"subtitle":"春游去哪里？南下赏花,‘地球的彩带、世界的花园’,畅游百里杜鹃花之乡！","prmtDiscount":0,"prmtRmk":""},{"currency":"RMB","dDateRmk":"每周三、四","prefFlag":"代理","flag":0,"imgUrls":"http://pkgpic.ctrip.com/images3/1/33/132x88/33_03_132x88.jpg","price":1550,"id":1721541,"name":"贵州黄果树+西江苗寨5日团队游(3钻)·青岩古镇+天河潭 当地游","prmt":{"amt":0,"currency":"RMB","list":[],"type":0},"subtitle":"精华景点一网打尽，精选酒店，让你旅途更加美满。","prmtDiscount":0,"prmtRmk":""},{"currency":"RMB","dDateRmk":"每周二、三...","flag":0,"imgUrls":"http://pkgpic.ctrip.com/images3/1/33/132x88/33_277_s22587_132x88.JPG","price":1399,"id":1628147,"name":"贵州黄果树+贵阳4日团队游(4钻)·南江峡谷+青岩古镇 当地游","prmt":{"amt":0,"currency":"RMB","list":[],"type":0},"subtitle":"洗涤都市人浮躁的栖息地,与大自然邂逅,带给您完全不一样的体验！最高立减200!","prmtDiscount":0,"prmtRmk":"早订优惠"},{"currency":"RMB","dDateRmk":"每周六","prefFlag":"代理","flag":0,"imgUrls":"http://pkgpic.ctrip.com/images3/1/1770/132x88/1770_14_s31519_132x88.JPG","price":398,"id":1732936,"name":"贵州赤水2日当地参团(2钻)·游走世遗风光 早订爽","prmt":{"amt":0,"currency":"RMB","list":[],"type":0},"subtitle":"观无限自然风光！感受红军四渡赤水经历！","prmtDiscount":0,"prmtRmk":"早订优惠"},{"currency":"RMB","dDateRmk":"每周五","prefFlag":"代理","flag":0,"imgUrls":"http://pkgpic.ctrip.com/images3/1/1770/132x88/1770_20_s38265_132x88.JPG","price":568,"id":1734817,"name":"贵州遵义+赤水3日当地参团(2钻)·穿行世界遗产 早订优惠多","prmt":{"amt":0,"currency":"RMB","list":[],"type":0},"subtitle":"一座红色之城，让心灵去旅行，走近她的活色生香，感受她的山清水秀……遵义！","prmtDiscount":0,"prmtRmk":""},{"currency":"RMB","dDateRmk":"每周六","prefFlag":"代理","flag":0,"imgUrls":"http://pkgpic.ctrip.com/images3/1/659/132x88/659_9_s30835_132x88.jpg","price":538,"id":1743563,"name":"荔波+西江苗寨2日团队游(3钻)·贵州精华 超值特惠","prmt":{"amt":0,"currency":"RMB","list":[],"type":0},"subtitle":"观赏荔波小七孔自然风光！体验西江苗族风情文化！","prmtDiscount":0,"prmtRmk":""},{"currency":"RMB","dDateRmk":"天天出发","prefFlag":"代理","flag":2,"imgUrls":"http://pkgpic.ctrip.com/images3/1/491/132x88/491_33_s22587_132x88.JPG","price":268,"id":1730130,"name":"贵阳西江千户苗寨纯玩一日游","prmt":{"amt":0,"currency":"RMB","list":[],"type":0},"subtitle":"游览中国乃至全世界最大的苗族聚居村寨，领略苗族漫长历史与发展的首选之地。","prmtDiscount":0,"prmtRmk":""}],"count":9,"ResponseStatus":{"Timestamp":"\/Date(1397809688549+0800)\/","Ack":"Success","Errors":[],"Extension":[]},"head":{"errcode":0}});
    console.log('responding json');
}

function snap(req, res, next)
{
    redisClient.get("counter",function(err,result){
        var counter = result;
        if(counter>=100)
        {
            res.send({"result":"promotion end"});
            console.log('promotion end');
        }
        else {
            counter = redisClient.incr("counter", function (err1, result1) {
                counter = result1;
                if (counter <= 100) {
                    console.log('%s is snapped', counter);
                    res.send({"result": counter});
                }
            });
        }
    });

    //res.contenttype='application/json;charset=utf-8';
    //console.log('responding json');
}

function logInnoDB(req,res,next){
    log(res,'bizLog_inno');
}
function logArchive(req,res,next){
    log(res,'bizLog_archive');
}


function log(res,tableName){
    dao.createLog(res,tableName);
}
function showTask(req,res,next){
    dao.showTask(res);
}

var numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
    // Fork workers.
    for (var i = 0; i < numCPUs; i++) {
        cluster.fork();
    }

    cluster.on('exit', function(worker, code, signal) {
        console.log('worker ' + worker.process.pid + ' died');
    });
}
else {
    var server = restify.createServer();
    
    server.get('/hola/:name', respond);
    server.get('/restapi/json/ProductListSearch', postRespond);
    server.post('/restapi/json/ProductListSearch', postRespond);
    server.post('/promotion', snap);
    server.get('/promotion',snap);
    server.get('/loginnodb',logInnoDB);
    server.get('/logarchive',logArchive);
//    server.get('/showtask',showTask);

    server.listen(3001, function () {
        console.log('rest_restify listening on port 3001');
    });
}